{% extends "edit/edit-base.html" %} 
{% block content %}
<form role="form">
  <div class="form-group">
    <label for="title"><h4><span class="label label-primary">标题</span></h4></label>
    <input type="text" class="form-control" id="title" placeholder="请输入文章标题。。。" value="{{ blog.title }}">
    <input type="hidden" class="form-control" id="blog_id" placeholder="blog_id" value="{{ blog.id }}">
  </div>
  
  <div class="form-group">
    <label for="summary"><h4><span class="label label-success">摘要</span></h4></label>
    <textarea type="text" class="form-control" id="summary" rows="4" placeholder="请输入文章摘要。。。">{{ blog.summary }}</textarea>
  </div>
  
  <div class="form-group">
    <label for="content"><h4><span class="label label-info">正文</span></h4></label>
    <!-- 加载编辑器的容器--开始 -->
    <script id="container" name="content" type="text/plain">{%autoescape off%}{{blog.content}}{%endautoescape%}</script>

    <!-- 实例化编辑器 -->
    <script type="text/javascript">
        var ue = UE.getEditor('container');
        
    </script>
    <!-- 加载编辑器的容器--完成 -->
  </div>
  
  <div class="form-group">
    <label for="keyword"><h4><span class="label label-warning">关键字</span></h4></label>
    <input type="text" class="form-control" id="tags" placeholder="为文章设置关键字，最多5个，用空格隔开。。。" 
    
    value="{% if tags %}{% for tag in tags %}{{tag.content}}&nbsp;{% endfor %}{%endif%}">
  </div>

	<div class="form-group">
		<label for="title"><h4><span class="label label-success">设置</span></h4></label>

		<div class="form-horizontal" role="form">
			<div class="form-group">
				<label for="inputPassword" class="col-sm-2 control-label">文章分类</label>
				<div class="col-sm-2">
					<select id="classify_id" class="form-control">
					{% for classify in classifys %}
						<option value="{{ classify.id }}" {%ifequal classify.id blog.classify_id %}selected{% endifequal %}>{{classify.content}}</option>
					{%endfor%}
					</select>
				</div>
			</div>
			
			<div class="form-group">
				<label for="inputPassword" class="col-sm-2 control-label">文章类型</label>
				<div class="col-sm-3">
					<label class="radio-inline">
						 <input type="radio" name="inlineRadioOptions" id="type" value="1" {%ifequal blog.type 1%} checked="true" {%endifequal%}> 原创
					</label>
					<label class="radio-inline">
						  <input type="radio" name="inlineRadioOptions" id="type" value="0" {%ifequal blog.type 0%} checked="true" {%endifequal%}> 转帖
					</label>
					<label class="radio-inline">
						  <input type="radio" name="inlineRadioOptions" id="type" value="2" {%ifequal blog.type 2%} checked="true" {%endifequal%}> 翻译
					</label>
				</div>
			</div>
		</div>
	</div>
<div class="form-group">

	 <!-- Provides extra visual weight and identifies the primary action in a set of buttons -->
	<button id="submit_release" type="button" class="btn btn-primary btn btn-primary btn-lg btn-block">提交</button>
{%ifequal blog.release 0%}
	<!-- Indicates a successful or positive action -->
	<button id="submit_draft" type="button" class="btn btn-success btn btn-primary btn-lg btn-block">保存草稿</button>
{%endifequal%}
</div>
</form>
<script type="text/javascript">
//全局参数，对应 blog 的各个字段
var param={
		id:"{{blog.id}}",
		title:"",
		summary:"",
		contnet:"",
		classify_id:"1",
		type:"1",
		tags:"",
		release:"1"
}

//获取编辑器中的文本内容
function getContent() {
	  param.content = UE.getEditor('container').getContent();
	  param.title = $("#title").val();
	  param.summary = $("#summary").val();
	  param.type = $("input[id='type']:checked").val();
	  param.classify_id = $("#classify_id").val();
	  param.tags = $("#tags").val();
}	

//保存新创建的文章
function  saveNewBlog(){
	$.post("/edit/save_update_blog/", param, function (result) { 
		if(result=="success"){
			alert("保存成功。。。");
			window.location.href='/edit/get_released_blog_list';
		}
	});
}
//自动摘要
function autoGetSummary(){
	$("#summary").val(UE.getEditor('container').getContentTxt().substring(0,250));
}
//检测标题，摘要，正文，关键字是否未填写
function checkEveryElement(){
	if(param.title==" " || param.title.length==0){
		alert("标题未填写。。。");
		$("#title").focus();
	}else if(param.summary==" " || param.summary.length==0){
		alert("摘要未填写。。。");
		if(confirm("是否自动生成摘要？")){autoGetSummary();}
		$("#summary").focus();
	}else if(param.content==" " || param.content.length==0){
		alert("文章正文未填写。。。");
	}else if(param.tags==" " || param.tags.length==0){
		alert("关键字未填写。。。");
		$("#tags").focus();
	}else{
		return true;
	}
	return false;
}

$(document).ready(function(){

//发布文章
  $("#submit_release").click(function(){
	getContent();
	param.release=1;
	//通过内容完整性验证
	if(checkEveryElement()){
		saveNewBlog();
	}
  });
//保存草稿
  $("#submit_draft").click(function(){
		getContent();
		param.release=0;
		if(checkEveryElement()){
			saveNewBlog();
		}
  });
	  
});
</script>
{% endblock %}
